import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home/home.vue'
import ClassifyView from '../views/classify/classify.vue'
import CartView from '../views/cart/cart.vue'
import AdminView from '../views/admin/admin.vue'
import LoginView from '../views/login/login.vue'
import RegisterView from '../views/register/register.vue'
import DetailView from '../views/detail/detail.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/classify',
    name: 'classify',
    component: ClassifyView
  },
  {
    path: '/cart',
    name: 'cart',
    component: CartView
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView
  },
  {
    path: '/detail',
    name: 'detail',
    component: DetailView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    // 路由守卫
    beforeEnter: (to, from, next) => {
      // 拿到登录状态
      const { islogin } = localStorage
      islogin? next({name:'home'}):next()
    }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    // 路由守卫
    beforeEnter: (to, from, next) => {
      // 拿到登录状态
      const { islogin } = localStorage
      islogin? next({name:'home'}):next()
    }
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router
